// pages/home/search/search.js
import {
  getSearchCourseList
} from '../../../api/course.js';
import {
  $wuxDialog
} from '../../../miniprogram_npm/wux-weapp/index.js';

Page({

  /**
   * 页面的初始数据
   */
  data: {
    keyword: '',
    historys: [],
    courseList: [],
    pageNo: 1,
    rowCount: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getHistoryRecord();
  },

  /**
   * 输入框输入
   */
  handleInput(e) {
    let keyword = e.detail.value;
    if (keyword === '') {
      this.setData({
        keyword
      })
    } else {
      this.data.keyword = keyword;
    }
  },

  handleSearchOrClear() {
    if (this.data.courseList.length !== 0) { // clear
      this.setData({
        keyword: '',
        courseList: []
      })
    } else {
      this.handleSearch({
        currentTarget: {
          dataset: {
            keyword: this.data.keyword
          }
        }
      });
    }
  },

  /**
   * 搜索
   */
  handleSearch(e) {
    let keyword = e.currentTarget.dataset.keyword;
    if (keyword != null) {
      this.setData({
        keyword
      })
    }
    if (keyword !== '') {
      this.data.pageNo = 1; // 复位
      this.data.courseList.length = 0;
      this.getSearchResult();
    } else {
      wx.showToast({
        icon: 'none',
        title: '请输入关键词进行搜索',
      })
    }
  },

  /**
   * 获取搜索内容
   */
  getSearchResult() {
    getSearchCourseList(this.data.keyword, this.data.pageNo).then(res => {
      this.setData({
        courseList: this.data.courseList.concat(res.list),
        rowCount: res.rowCount,
      })
      if (res.list.length === 0) {
        wx.showToast({
          icon: 'none',
          title: '该关键词没有相应内容，请重新输入...',
        })
        return false;
      } else if (this.data.pageNo > 1 || this.data.keyword === '') {
        return false;
      } else {
        true
      }
    }).then((res) => {
      console.log(res)
      if (res && this.data.historys.indexOf(this.data.keyword) === -1) {
        if (this.data.historys.length >= 5) { // 只保留 5 条数据
          this.data.historys.unshift(this.data.keyword);
          this.data.historys.pop();
        } else {
          this.data.historys.unshift(this.data.keyword);
        }
        this.setData({
          historys: this.data.historys
        })
      }
    })
  },

  /**
   * 清除记录
   */
  handleClear() {
    let that = this;
    $wuxDialog().confirm({
      title: '警告',
      content: '是否删除全部历史记录？',
      buttons: [{
        text: '取消',
      }, {
        text: '确认',
        type: 'primary',
        onTap(e) {
          that.setData({
            historys: []
          })
        }
      }],
    })
  },

  /**
   * 删除某条记录
   */
  handleDelete(e) {
    let keyword = e.currentTarget.dataset.keyword;
    let that = this;
    $wuxDialog().confirm({
      title: keyword,
      content: '是否删除此条记录？',
      buttons: [{
        text: '取消',
      }, {
        text: '确认',
        type: 'primary',
        onTap(e) {
          let historys = that.data.historys.filter(item => keyword !== item);
          that.setData({
            historys
          })
        }
      }],
    })
  },

  /**
   * 获取历史记录
   */
  getHistoryRecord() {
    wx.getStorage({
      key: 'history',
      success: (res) => {
        this.setData({
          historys: res.data
        })
      },
    })
  },

  /**
   * 更新历史记录
   */
  updateHistoryRecord() {
    wx.setStorage({
      key: 'history',
      data: this.data.historys,
    })
  },

  /**
   * 跳转课程详情
   * @param {object} e 
   */
  navigatorCourse(e) {
    wx.navigateTo({
      url: '/pages/learn/course/details?id=' + e.detail,
    })
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    this.updateHistoryRecord();
  },

  /**
   * 滑动到底部
   */
  handleScrollBottom(e) {
    if (this.data.rowCount > this.data.courseList.length) {
      this.data.pageNo += 1;
      this.getSearchResult();
    }
  },

})